package com.example.myapplication

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.ProgressBar
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout


class MainActivity : AppCompatActivity() {
    /** 使用帧动画实现的loading */
    private var frameAnimLoading: ImageView? = null
    /** 使用补间动画实现的loading */
    private var rotateAnimLoading: ImageView? = null

    private var rootView: FrameLayout? = null

    /** RecycleView 实例 */
    private var recycleView: RecyclerView? = null

    /** RecycleView 的适配器 */
    private var adapter: DemoAdapter? = null

    var username: String? = null

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        addRecycleView()
//        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
//        val editText = findViewById<EditText>(R.id.et1)
//        editText.setOnTouchListener { v, event ->
//            v.parent.requestDisallowInterceptTouchEvent(true)
//            false
//        }
        val tabLayout: TabLayout = findViewById(R.id.tabLayout)
        val viewPager: ViewPager = findViewById(R.id.viewPager)
        if (loginflag.sharedValue==0) {
            val fragments = listOf(shouye(), shipin(), wode()) // 替换为你自己的Fragment

            val titles = listOf("首页", "视频", "我的(未登录)") // 替换为你自己的标签名
            val icons = listOf(R.drawable.home2, R.drawable.shipin, R.drawable.wode) // 替换为你自己的图标资源
            val pagerAdapter = MyPagerAdapter(supportFragmentManager, fragments, titles)
            viewPager.adapter = pagerAdapter
            tabLayout.setupWithViewPager(viewPager)
            for (i in 0 until tabLayout.tabCount) {
                tabLayout.getTabAt(i)?.setIcon(icons[i])
            }
        }
        else{
            val fragments = listOf(shouye(), shipin(), loginwode()) // 替换为你自己的Fragment

            val titles = listOf("首页", "视频", "我的") // 替换为你自己的标签名
            val icons = listOf(R.drawable.home2, R.drawable.shipin, R.drawable.wode) // 替换为你自己的图标资源
            val pagerAdapter = MyPagerAdapter(supportFragmentManager, fragments, titles)
            viewPager.adapter = pagerAdapter
            tabLayout.setupWithViewPager(viewPager)
            for (i in 0 until tabLayout.tabCount) {
                tabLayout.getTabAt(i)?.setIcon(icons[i])
            }
        }




    }


    private fun addRecycleView() {
        // 从布局中进行获取
        recycleView = findViewById(R.id.rv_demo)
        adapter = DemoAdapter(createDemoDate())
        recycleView?.adapter = adapter
        recycleView?.layoutManager = LinearLayoutManager(this)
    }

    private fun createDemoDate(): ArrayList<ItemBean> {

        val list = ArrayList<ItemBean>()

        val Item1 = ItemBean(R.drawable.a, "这些重大问题，习近平的回答掷地有声", "新华社新媒体")
        val Item2 =
            ItemBean(R.drawable.b, "习近平会见“元老会”代表团", "央视网新闻")

        val Item3 = ItemBean(R.drawable.c, "美媒：五角大楼盯上谷歌在华AI中心 谷歌忙安抚", "参考消息")
        val Item4 = ItemBean(R.drawable.d, "蔡英文财产曝光：存款5406万 名下拥6笔不动产", "人民日报海外网 120评论")
        for (i in 0 until 3) {
            list.add(Item1)
            list.add(Item2)
            list.add(Item3)
            list.add(Item4)
        }

        return list
    }


    private val LOADING_DELAY: Long = 2000 // 300ms
    private lateinit var progressBar: ProgressBar

    // 点击事件处理方法
    fun onTextViewClick(view: View) {
        // 显示 loading 界面
        progressBar = findViewById(R.id.progressBar1)
        progressBar.visibility = View.VISIBLE
        val linearLayout1 =findViewById<LinearLayout>(R.id.ll5)
        linearLayout1?.setBackgroundColor(Color.WHITE)

        // 延迟 300ms 后跳转到目标 Activity
        Handler(Looper.getMainLooper()).postDelayed({
            // 隐藏 loading 界面
            progressBar.visibility = View.GONE

            // 跳转到目标 Activity
            val intent = Intent(this, weather::class.java)
            startActivity(intent)


        }, LOADING_DELAY) // 延迟 300ms
    }

    fun onTextViewClick2(view: View) {
            val intent = Intent(this, addnews::class.java)
            startActivity(intent)

    }
    fun onTextViewClick3(view: View) {
        val intent = Intent(this, LoginActivity::class.java)
        startActivity(intent)

    }
    fun onTextViewClick4(view: View) {
        val intent = Intent(this, SearchActivity::class.java)
        startActivity(intent)

    }



}
